package org.example.blog.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.example.blog.Mapper.IndexMapper;
import org.example.blog.pojo.ArticleMs;
import org.example.blog.pojo.FastNavigation;
import org.example.blog.pojo.PageBean;
import org.example.blog.pojo.Slideshow;
import org.example.blog.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class IndexServiceImpl implements IndexService {

    @Autowired
    private IndexMapper indexMapper;


    @Override
    public List<FastNavigation> list() {
        return indexMapper.list();
    }

    @Override
    public void add(FastNavigation fastNavigation) {
        indexMapper.insert(fastNavigation);
    }

    @Override
    public void delete(Integer id) {
        indexMapper.deleteById(id);
    }

    @Override
    public void update(FastNavigation fastNavigation) {
        indexMapper.update(fastNavigation);
    }

    @Override
    public void upload(String path) {
        indexMapper.add1(path);
    }

    @Override
    public List<Slideshow> list1() {
        return indexMapper.list1();

    }

    @Override
    public void delete1(Integer id) {
        indexMapper.deleteById1(id);
    }

    @Override
    public void update1(Slideshow slideshow) {
        indexMapper.update1(slideshow);
    }

    @Override
    public PageBean list2(Integer page, Integer pageSize) {

        // 1.设置分页参数
//        PageHelper.startPage(page,pageSize);
//        // 2.执行查询
//        List<ArticleMs> list = indexMapper.list2();
//        Page<ArticleMs> p = (Page<ArticleMs>) list;
//        // 3.封装PageBean对象
//        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());

        // 1.获取全部数据
        List<ArticleMs> list = indexMapper.list2();
        
        // 2.计算分页参数
        int totalSize = list.size();
        log.info("total:{}",totalSize);
        int startIndex = (page - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize , totalSize);
        
        // 3.处理边界情况
        if (startIndex >= totalSize) {
            return null;
        }
        
        // 4.返回分页后的数据
        PageBean pageBean = new PageBean(totalSize,list.subList(startIndex, endIndex));
        return pageBean;
    }

    @Override
    public void add2(ArticleMs articleMs) {
        //生成更改时间
        articleMs.setUpdateTime(LocalDateTime.now());
        //添加文章
        indexMapper.addArticle(articleMs);
        //通过时间获取生成id
        Integer articleId = indexMapper.getArticleId();
        log.info("articleId:{}",articleId);
        //通过id更改标签
        indexMapper.addTag(articleMs.getTags(),articleId);
    }

    @Override
    public void deleteById(Integer id) {
        indexMapper.deleteArticleById(id);
        indexMapper.deleteTagById(id);
    }

    @Override
    public void update2(ArticleMs articleMs) {
        //1.修改文章
        indexMapper.update2(articleMs);
        //2.修改标签
        // a.通过文章id删除文章标签
        indexMapper.deleteTagById(articleMs.getId());
        // b.通过文章id添加文章标签
        indexMapper.addTag(articleMs.getTags(),articleMs.getId());
    }

    //修改点击量
    @Override
    public void update3(Integer id, Integer hits) {
        ArticleMs articleMs = new ArticleMs();
        articleMs.setId(id);
        articleMs.setHits(hits);
        indexMapper.update2(articleMs);
    }

}
